<template>
  <view v-if="goodsInfo.goods_name" class="goods-detail-container">
    <!-- 轮播图区域 -->
    <swiper class="swiper-box" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(goods_pics,i) in goodsInfo.pics" :key="i">
        <view class="swiper-item">
          <image :src="goods_pics.pics_big" mode="widthFix" @click="preview(i)"></image>
        </view>
      </swiper-item>
    </swiper>
    <!-- 商品价格和商品标题区域 -->
    <view class="goods_price_title">
      <view class="goods_price">
        ￥{{goodsInfo.goods_price}}
      </view>
      <view class="goods_middle">
        <view class="goods_title">
          {{goodsInfo.goods_name}}
        </view>
        <view class="goods_collect">
          <uni-icons type="star" size="24" color="#aaa"></uni-icons><br>
          收藏
        </view>
      </view>
      <view class="goods_express">
        快递：免运费
      </view>
    </view>
    <!-- 商品图文介绍区域 -->
    <view class="goods_introduce">
      <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    </view>
    <!-- 商品导航区域 -->
    <view class="goods_nav">
      <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
  export default {
    computed:{
      ...mapGetters('m_cart',['total'])
    },
    watch:{
      total:{
        handler(newVal){
          const result=this.options.find(x=>x.text==='购物车')
          if(result){
          result.info=newVal
          }
        },
        immediate:true
      }
    },
    data() {
      return {
        goodsInfo:[],
        options:[
          {
            icon:'shop',
            text:'店铺'
          },
          {
            icon:'cart',
            info:0,
            text:'购物车'
          }
        ],
        buttonGroup:[
          {
            text:'加入购物车',
            backgroundColor:'#ff0000',
            color:'#fff'
          },
          {
            text:'立即购买',
            backgroundColor:'#ffa200',
            color:'#fff'
          }
        ]
      }
    },
    methods:{
      // 获取商品详情
      async _getGoodsDetail(goods_id){
        const {data:res}=await uni.$http.get('/api/public/v1/goods/detail',goods_id)
        if(!res||res.meta.status!==200)return uni.$showMsg('数据加载失败！')
        res.message.goods_introduce=res.message.goods_introduce.replace(/<img /g,'<img style="display:block;"').replace(/.webp/g,'.jpg')
        this.goodsInfo=res.message
        // console.log(res)
      },
      preview(i){
        uni.previewImage({
          current:i,
          urls:this.goodsInfo.pics.map(x=>x.pics_big)
        })
      },
      onClick (e) {
        if(e.content.text==='购物车'){
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      buttonClick (e) {
        if(e.content.text==="加入购物车"){
          const goods={
            goods_id:this.goodsInfo.goods_id,                 //商品ID
            goods_name:this.goodsInfo.goods_name,             //商品名称
            goods_price:this.goodsInfo.goods_price,           //商品单击
            goods_count:1,                                    //加入购物车数量
            goods_small_logo:this.goodsInfo.goods_small_logo, //商品小logo
            goods_state:true                                  //在购物车中的选中状态
          }
          this.addToCart(goods)
        }
      },
      ...mapMutations('m_cart',['addToCart'])
    },
    onLoad(options) {
      this._getGoodsDetail(options)
    }
    
  }
</script>

<style lang="scss">
.swiper-box{
  height: 750rpx;
  image{
    width: 100%;
    height: 100%;
  }
}
.goods_price_title{
  >view{
    margin: 10px 10px;
  }
  .goods_price{
    font-size: 18px;
    color: #E16531;
    font-weight: bold;
    transform: scaleY(1.2);
  }
  .goods_middle{
    display: flex;
    font-size: 14px;
    .goods_title{
      width: 75%;
      padding-right: 10px;
      border-right: 1px solid #eee;
    }
    .goods_collect{
      flex: 1;
      text-align: center;
      color: #aaa;
      font-size: 13px;
    }
  }
  .goods_express{
    font-size: 13px;
    color: #aaa;
  }
}
.goods_nav{
  position: sticky;
  bottom: 0;
  z-index: 999;
}
</style>
